/* Create a Class profile .. Temp Storage using arrays */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

#define M 50
int menu();

void StudentDetailsEnter();
void StudentDetailsDisplay();
void Report();
int rno[M],srno[50],ch,ch1,rank[50],i,rollch,ei=0;
char name[M][30],sname[M][30],bool;
float mat[M],smat[M],sphy[M],phy[M],total[M],stotal[M],perc[M];//chem[M],bio[M],his[M],comp[M],total[M],perc[M];

void main()
{

  clrscr();
  start:
  ch = menu();
  printf("%d",ch);

  switch(ch)
  {
    case 0 : exit(0);
    case 1 : StudentDetailsEnter();
	     break;

    case 2 : StudentDetailsDisplay();
	     break;

    case 3 : Report();break;
  }
  getch();
}

int menu()
{
  int ch;
  printf("MENU\n");
  printf("1.Enter Student Details\n ");
  printf("2.Display Student Details\n ");
  printf("3.Show Report Card\n");
  printf("4.All Students Performance\n");
  scanf("%d",&ch);
  printf("choice in menu %d",ch);
  return ch;

}

void StudentDetailsEnter()
{
	     for(;ei<50;)
	     {
	       printf("\nEnter the roll number : ");
	       scanf("%d",&rno[ei]);
	       printf("\nEnter the Name of student :");
	       fflush(stdin);
	       gets(name[ei]);
	       printf("\nEnter the marks of maths , Phys :");
	       scanf("%f%f",&mat[ei],&phy[ei]);
	       ei++;
	       printf("\n Detailno : %d ",ei);
	       printf("\nAnother record ? ");
	       bool=getche();
	       if(bool == 'n')
	       {
		printf("\n\tTest Into CASE 1\t\n ");
	       //	menu();
		main();
	       }
	      }

}

void StudentDetailsDisplay()
{
	    option2:
	    clrscr();
	    printf("\n1.One Student Details ? ");
	    printf("\n2.All Students Details ?\n ");
	    scanf("%d",&ch1);
	    switch(ch1)
	    {
	      case 1 : printf("\n One Student Details --> ");
		       printf("\n Enter the roll no of the student : ");
		       scanf("%d",&rollch);
		       for(i=0;i<50;i++)
		       {
			 if(rollch == rno[i])
			 {
			   printf("Roll no : %d \n Name : %s \n Maths : %.2f \n Phy : %.2f \n Total : %.2f ",rno[i],name[i],mat[i],phy[i],(mat[i]+phy[i]));
			   rollch=100;
			 }
		       }
		       if(rollch!=100)
			printf("\nSorry Record not found ");
			printf("\n 0.exit \n 1. main menu \n 2.This menu \n ");
			scanf("%d",&ch1);
			switch(ch1)
			{
			  case 1 : main();
				   //break;
			  case 2 : StudentDetailsDisplay();
				   //break;
			  case 0 : exit(0);
			}

	      case 2 : printf("All Student Details \nRoll\tName\tMaths\tPhysics\tTotal\n" );
		       for(i=0;i<50;i++)
		       {

			 if((rno[i]==0) && (name[i][0]=='\0'))
			 goto out;


			 total[i]=mat[i]+phy[i];
			 printf("%d\t%s\t%f\t%f\t%f\n",rno[i],name[i],mat[i],phy[i],total[i]);

		       }
		       out:
		       printf("\n Press any key to continue ");
		       getch();
		       printf("\n1.Main Menu ? \n 2.This Menu? \n 0.Exit");
		       scanf("%d",&ch1);
		       switch(ch1)
		       {
			 case 1 : main();break;
			 case 2 : StudentDetailsDisplay(); break;
			 case 0 : exit(0);
			default : printf("\n Invalid option ..Re enter option . (Press to conti)  ");
				   getch();
				   goto out;
				   //goto invalid;
		       }

	     }

}

void Report()
{

  int count=0;
  for(int temp=0;temp<M;temp++)
   {
    total[temp]=mat[temp]+phy[temp];
    if(name[temp][1]!='\0')
    count++;
   }
   printf("\n Count of array is : %d",count);


   // for(int add=0;add<count;add++)
     //{
      //total[add]=mat[add]+phy[add];
      //printf("\n name : %s , no : %d , mat : %.2f , phy : %.2f , total : %.2f ",name[add],rno[add],mat[add],phy[add],total[add]);
    // }


  // Bubble Sort .. Dropped from Internet

   int outer,inner,tempo;
   float tempf;
   char temps[30];
   for(outer=0;outer<(count-1);++outer)
   {
    for(inner=0;inner<(count-1-outer);++inner)
     {
     if(total[inner]>total[inner+1])
       {
	 tempo=rno[inner];
	 rno[inner]=rno[inner+1];
	 rno[inner+1]=tempo;
	 //name
	 strcpy(temps,name[inner]);
	 strcpy(name[inner],name[inner+1]);
	 strcpy(name[inner+1],temps);
	 //mat
	 tempf=mat[inner];
	 mat[inner]=mat[inner+1];
	 mat[inner+1]=tempf;
	 //phy
	 tempf=phy[inner];
	 phy[inner]=phy[inner+1];
	 phy[inner+1]=tempf;
       }
     }
   }
     for(int add=0;add<count;add++)
     {
      total[add]=mat[add]+phy[add];
      printf("\n name : %s , no : %d , mat : %.2f , phy : %.2f , total : %.2f ",name[add],rno[add],mat[add],phy[add],total[add]);
     }





  getch();
  main();

}